package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.core.util.StateNum;
import com.woniuxy.goods.dto.GoodsCatDto;
import com.woniuxy.goods.dto.GoodsCatTreeDto;
import com.woniuxy.goods.entity.GoodsBrandCat;
import com.woniuxy.goods.entity.GoodsCat;
import com.woniuxy.goods.form.AddGoodsCatForm;
import com.woniuxy.goods.form.UpdateCatPictureForm;
import com.woniuxy.goods.form.UpdateCatStateForm;
import com.woniuxy.goods.form.UpdateGoodsCatForm;
import com.woniuxy.goods.param.*;
import com.woniuxy.goods.service.GoodsCatService;
import com.woniuxy.web.util.ConvertUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 * 类别管理控制层接口
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@Api(tags = "类别管理接口")
@RequestMapping("/goodsCat")
public class GoodsCatController {
    /**
     * 类别管理服务层接口
     */
    @Resource
    private GoodsCatService goodsCatService;

    /**
     * 新增典当物品类别
     * @param form 新增类别对象
     * @return 类别信息
     * @throws Exception
     */
    @ApiOperation("新增典当物品类别")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCat.class)
    })
    @PostMapping("/addGoodsCat")
    @Permission(roles = {"超级管理员","管理员"})
    public Result addGoodsCat (@Validated @RequestBody AddGoodsCatForm form, BindingResult br)throws Exception{
        if(br.hasErrors()) throw new ParamException(10001,"参数有误");
        AddGoodsCatParam param = BeanUtil.toBean(form, AddGoodsCatParam.class);
        List<AddCatPictureParam> params = ConvertUtil.convertListTo(form.getForms(), AddCatPictureParam::new);
        param.setParams(params);
        GoodsCat goodsCat = goodsCatService.addGoodsCat(param);
        return Result.success("新增典当物品类别成功",goodsCat);
    }

    /**
     * 修改典当物品类别
     * @param form 修改类别对象
     * @return 类别信息
     * @throws Exception
     */
    @ApiOperation("修改典当物品类别")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCat.class)
    })
    @PostMapping("/updateGoodsCat")
    @Permission(roles = {"超级管理员","管理员"})
    public Result updateGoodsCat ( @Validated @RequestBody UpdateGoodsCatForm form,BindingResult br)throws Exception{
        if(br.hasErrors()) throw new ParamException(10001,"参数有误");
        UpdateGoodsCatParam param = BeanUtil.toBean(form, UpdateGoodsCatParam.class);
        List<AddCatPictureParam> params = ConvertUtil.convertListTo(form.getForms(), AddCatPictureParam::new);
        param.setParams(params);
        GoodsCat goodsCat = goodsCatService.updateGoodsCat(param);
        return Result.success("修改典当物品类别成功",goodsCat);
    }

    /**
     * 修改典当物品类别状态
     * @param form 修改类别状态对象
     * @return 类别信息
     * @throws Exception
     */
    @ApiOperation("修改典当物品类别状态")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCat.class)
    })
    @PostMapping("/updateCatState")
    @Permission(roles = {"超级管理员","管理员"})
    public Result updateCatState (@Validated @RequestBody UpdateCatStateForm form,BindingResult br)throws Exception{
        if(br.hasErrors()) throw new ParamException(10001,"参数有误");
        GoodsCat goodsCat = goodsCatService.updateCatState(BeanUtil.toBean(form, UpdateCatStateParam.class));
        return Result.success("修改典当物品类别状态成功",goodsCat);
    }

    /**
     * 根据id查找典当物品类别
     * @param catId 类别id
     * @return 类别信息
     * @throws Exception
     */
    @ApiOperation("根据id查找典当物品类别")
    @ApiImplicitParam("类别id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCat.class)
    })
    @GetMapping("/findCatById")
    public Result findCatById (Integer catId)throws Exception{
        GoodsCat dto = goodsCatService.findCatById(catId);
        return Result.success("根据id查找典当物品类别成功",dto);
    }

    /**
     * 查找典当物品类别树
     * @return 类别信息树 （一级类别、二级类别、属性组）
     * @param brandId 品牌id
     * @throws Exception
     */
    @ApiOperation("查找典当物品类别树")
    @ApiImplicitParam("品牌id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatTreeDto.class)
    })
    @GetMapping("/findCatTree")
    public Result findCatTree (Integer brandId)throws Exception{
        List<GoodsCatTreeDto> tree = goodsCatService.findCatTree(brandId);
        return Result.success("查找典当物品类别树成功",tree);
    }

    /**
     * 通过ids查询典当物品类别
     * @param catIds 类别id集合
     * @return 类别信息集合
     * @throws Exception
     */
    @ApiOperation("通过ids查询典当物品类别")
    @ApiImplicitParam("类别id集合")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatDto.class)
    })
    @PostMapping("/findCatByIds")
    public Result findCatByIds(@RequestBody Set<Integer> catIds)throws Exception{
        List<GoodsCatDto> cats = goodsCatService.findCatByIds(catIds);
        return Result.success("通过ids查询典当物品类别成功",cats);
    }

    /**
     * 通过ids查询典当物品一级类别
     * @param catIds 类别id集合
     * @return 类别信息集合
     * @throws Exception
     */
    @ApiOperation("通过ids查询典当物品一级类别")
    @ApiImplicitParam("类别id集合")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCat.class)
    })
    @PostMapping("/findOneCatByIds")
    public Result findOneCatByIds(@RequestBody Set<Integer> catIds)throws Exception{
        List<GoodsCat> cats = goodsCatService.findOneCatByIds(catIds);
        return Result.success("通过ids查询典当物品一级类别成功",cats);
    }

    /**
     * 查找所有典当物品类别
     * @return 类别信息
     * @throws Exception
     */
    @ApiOperation("查找所有典当物品类别")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatDto.class)
    })
    @GetMapping("/findAllCat")
    public Result findAllCat ()throws Exception{
        List<GoodsCatDto> dto = goodsCatService.findAllCat();
        return Result.success("查找所有典当物品类别成功",dto);
    }

    /**
     * 分页查询所有一级类别
     * @param pageNum
     * @param pageSize
     * @param catName
     * @return
     */
    @ApiOperation("分页查询所有一级类别")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatDto.class)
    })
    @GetMapping("/pageAllCat")
    @ApiImplicitParams({
         @ApiImplicitParam(name="pageNum",value = "当前页数"),
         @ApiImplicitParam(name="pageSize",value = "每页几条"),
         @ApiImplicitParam(name="catName",value = "类别名"),
    })
    @Permission(roles = {"超级管理员","管理员"})
    public Result pageAllCat(Integer pageNum,Integer pageSize,String catName) throws Exception {
        Page<GoodsCatDto> pages = goodsCatService.pageAllCat(pageNum,pageSize,catName);
        return Result.success("查找所有典当物品类别成功",pages);
    }

    /**
     * 查找所有二级类别
     * @return 类别信息
     * @throws Exception
     */
    @ApiOperation("查找所有二级类别")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatDto.class)
    })
    @GetMapping("/findAllSubCat")
    public Result findAllSubCat ()throws Exception{
        List<GoodsCat> dto = goodsCatService.findAllSubCat();
        return Result.success("查找所有二级类别成功",dto);
    }

}

